package com.lancoo.ccas53.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas53.entity.Room;
import com.lancoo.ccas53.entity.RoomType;
import com.lancoo.ccas53.entity.TeachingClassWeek;
import com.lancoo.ccas53.exception.BizException;
import com.lancoo.ccas53.mapper.RoomTypeMapper;
import com.lancoo.ccas53.pojo.vo.RoomTypeVo;
import com.lancoo.ccas53.service.RoomService;
import com.lancoo.ccas53.service.RoomTypeService;
import com.lancoo.ccas53.service.TeachingClassWeekService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoomTypeServiceImpl extends ServiceImpl<RoomTypeMapper, RoomType> implements RoomTypeService {
    @Resource
    private RoomTypeMapper roomTypeMapper;
    @Resource
    private RoomService roomService;
    @Resource
    private TeachingClassWeekService teachingClassWeekService;

    @Override
    public List<RoomTypeVo> getRoomTypeList(Integer taskId) {
        List<RoomTypeVo> roomTypeVoList = new ArrayList<>();
        List<RoomType> list = this.lambdaQuery().eq(RoomType::getTaskId, taskId).list();
        list.stream().forEach(a -> {
            RoomTypeVo roomTypeVo = new RoomTypeVo();
            BeanUtils.copyProperties(a, roomTypeVo);
            roomTypeVoList.add(roomTypeVo);
        });
        return roomTypeVoList;
    }

    /**
     * 场地类型id存在则为删除操作，场地名称存在则为添加操作
     *
     * @param taskId
     * @param id
     * @param roomName
     * @return
     */
    @Override
    public Boolean updateRoomType(Integer taskId, Long id, String roomName) {
        if (id == null) {
            //场地id不存在，为添加操作
            if (!StringUtils.isEmpty(roomName)) {
                RoomType one = this.lambdaQuery().eq(RoomType::getTaskId, taskId).eq(RoomType::getRoomName, roomName).one();
                if (one == null) {
                    //添加操作
                    RoomType roomType = new RoomType();
                    roomType.setTaskId(taskId);
                    roomType.setRoomName(roomName);
                    roomTypeMapper.insert(roomType);
                } else {
                    throw new BizException("该场地名称已存在！");
                }
            }
        } else {
            //场地id存在，为删除操作
            RoomType one = this.lambdaQuery().eq(RoomType::getTaskId, taskId).eq(RoomType::getId, id).one();
            if (one != null) {
                //删除操作前先根据id查询teaching_class_week表和room表对应的room_type参数
                List<Room> list = roomService.lambdaQuery().eq(Room::getRoomType, id).list();
                List<TeachingClassWeek> list1 = teachingClassWeekService.lambdaQuery().eq(TeachingClassWeek::getRoomType, id).list();
                if ((list == null || list.isEmpty()) && (list1 == null || list1.isEmpty())) {
                    roomTypeMapper.deleteById(one);
                } else {
                    throw new BizException("该场地正在被使用！");
                }
            } else {
                throw new BizException("该场地已被删除！");
            }
        }
        return true;
    }
}
